//
//  NIVehicleDataManager.m
//  AIWAYS
//
//  Created by Marx on 2018/8/23.
//  Copyright © 2018年 wlq. All rights reserved.
//

#import "NIVehicleDataManager.h"

@implementation NIVehicleDataManager

-(BOOL)saveVehicleData:(NIVehicleObject *)vehicle
{
    NIVehicleObject * vehicleData = [NIVehicleObject new];
    vehicleData.userId = vehicle.userId;
    vehicleData.vin = vehicle.vin;
    NSArray *array = [self selectCoreDataWithModel:vehicleData AndName:TABLE_NAME];
    if (array.count > 0) {
        NIVehicleObject *vehicleObject = (NIVehicleObject *)[array objectAtIndex:0];
        vehicle.gesturePwd = vehicleObject.gesturePwd;
        vehicle.verifyFingerPwd = vehicleObject.verifyFingerPwd;
        vehicle.verifyGesturePwd = vehicleObject.verifyGesturePwd;
        
    }
    return [self updateCoreDataWithOriginalModel:vehicleData andPresentModel:vehicle AndName:TABLE_NAME];
}

-(NIVehicleObject *)loadVehicleDataWithVin:(NSString *)vin userId:(NSString *)userId
{
    NIVehicleObject * vehicleData = [NIVehicleObject new];
    vehicleData.userId = userId;
    vehicleData.vin = vin;
    NSArray *array = [self selectCoreDataWithModel:vehicleData AndName:TABLE_NAME];
    if (array.count > 0) {
        return (NIVehicleObject *)[array objectAtIndex:0];
    }
    else
    {
        NSLog(@"未查到vin = %@ userid = %@ 对应的数据",vin,userId);
        return nil;
    }
}


-(BOOL)saveSecurity:(NSString *)securityState userId:(NSString *)userId vin:(NSString *)vin
{
    NIVehicleObject * vehicleData = [NIVehicleObject new];
    vehicleData.userId = userId;
    vehicleData.vin = vin;
    NSArray *array = [self selectCoreDataWithModel:vehicleData AndName:TABLE_NAME];
    if (array.count > 0) {
        NIVehicleObject *vehicleObject = (NIVehicleObject *)[array objectAtIndex:0];
        vehicleObject.securityState = securityState;
        return [self updateCoreDataWithOriginalModel:vehicleData andPresentModel:vehicleObject AndName:TABLE_NAME];
    }
    else
    {
        NSLog(@"未查到vin = %@ 对应的数据",vin);
        return NO;
    }
}


-(BOOL)saveGesturePwd:(NSString *)gesturePwd userId:(NSString *)userId vin:(NSString *)vin
{
    NIVehicleObject * vehicleData = [NIVehicleObject new];
    vehicleData.userId = userId;
    vehicleData.vin = vin;
    NSArray *array = [self selectCoreDataWithModel:vehicleData AndName:TABLE_NAME];
    if (array.count > 0) {
        NIVehicleObject *vehicleObject = (NIVehicleObject *)[array objectAtIndex:0];
        vehicleObject.gesturePwd = gesturePwd;
        return [self updateCoreDataWithOriginalModel:vehicleData andPresentModel:vehicleObject AndName:TABLE_NAME];
    }
    else
    {
        NSLog(@"未查到vin = %@ 对应的数据",vin);
        return NO;
    }
}



-(BOOL)saveVerifyFingerPwd:(NSString *)verifyFingerPwd userId:(NSString *)userId vin:(NSString *)vin
{
    NIVehicleObject * vehicleData = [NIVehicleObject new];
    vehicleData.userId = userId;
    vehicleData.vin = vin;
    NSArray *array = [self selectCoreDataWithModel:vehicleData AndName:TABLE_NAME];
    if (array.count > 0) {
        NIVehicleObject *vehicleObject = (NIVehicleObject *)[array objectAtIndex:0];
        vehicleObject.verifyFingerPwd = verifyFingerPwd;
        return [self updateCoreDataWithOriginalModel:vehicleData andPresentModel:vehicleObject AndName:TABLE_NAME];
    }
    else
    {
        NSLog(@"未查到vin = %@ 对应的数据",vin);
        return NO;
    }
}



-(BOOL)saveVerifyGesturePwd:(NSString *)verifyGesturePwd userId:(NSString *)userId vin:(NSString *)vin
{
    NIVehicleObject * vehicleData = [NIVehicleObject new];
    vehicleData.userId = userId;
    vehicleData.vin = vin;
    NSArray *array = [self selectCoreDataWithModel:vehicleData AndName:TABLE_NAME];
    if (array.count > 0) {
        NIVehicleObject *vehicleObject = (NIVehicleObject *)[array objectAtIndex:0];
        vehicleObject.verifyGesturePwd = verifyGesturePwd;
        return [self updateCoreDataWithOriginalModel:vehicleData andPresentModel:vehicleObject AndName:TABLE_NAME];
    }
    else
    {
        NSLog(@"未查到vin = %@ 对应的数据",vin);
        return NO;
    }
}
@end
